package com.pshcp.patient.task;

import com.external.common.enums.QuartzGroupEnum;
import com.pshcp.patient.task.job.OnlineOrderRefundStatusQueryJob;
import com.pshcp.patient.task.job.RegOrderRefundPayFeeJob;
import com.pshcp.patient.task.job.RegOrderRefundRetryJob;
import com.pshcp.patient.task.job.RegOrderRefundStatusQueryJob;
import com.pshcp.patient.task.job.RegOrderUnPayCancelJob;
import com.pshcp.patient.task.job.RegOrderUnPayMsgJob;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 定时任务创建
 *
 * @author ww
 */
@Configuration
public class PatientScheduledJobConfig {

    /**
     * 调用HIS系统补偿退号（5分钟一次）
     *
     * @return
     */
    @Bean("regOrderRefundRetryJobDetail")
    public JobDetail regOrderRefundRetryJobDetail() {
        return JobBuilder.newJob().ofType(RegOrderRefundRetryJob.class)
                .storeDurably()
                .withIdentity("RegOrderRefundRetryJob")
                .withDescription("调用HIS系统取消挂号订单")
                .build();
    }

    /**
     * 调用HIS系统补偿退号（5分钟一次）
     *
     * @param job
     * @return
     */
    @Bean
    public Trigger regOrderRefundRetryJobTrigger(@Qualifier("regOrderRefundRetryJobDetail") JobDetail job) {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("RegOrderRefundRetryJob", QuartzGroupEnum.DEFAULT_GROUP.getVal())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 */5 * * * ?"))
                .build();
        return trigger;
    }

    /**
     * 超时取消挂号订单（1分钟一次）
     *
     * @return
     */
    @Bean("regOrderUnPayCancelJobDetail")
    public JobDetail regOrderUnPayCancelJobDetail() {
        return JobBuilder.newJob().ofType(RegOrderUnPayCancelJob.class)
                .storeDurably()
                .withIdentity("RegOrderUnPayCancelJob")
                .withDescription("超时取消挂号订单")
                .build();
    }

    /**
     * 超时取消挂号订单（1分钟一次）
     *
     * @param job
     * @return
     */
    @Bean
    public Trigger regOrderUnPayCancelJobTrigger(@Qualifier("regOrderUnPayCancelJobDetail") JobDetail job) {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("RegOrderUnPayCancelJob", QuartzGroupEnum.DEFAULT_GROUP.getVal())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 */1 * * * ?"))
                .build();
        return trigger;
    }

    /**
     * 未支付挂号订单通知（1分钟一次）
     *
     * @return
     */
    @Bean("regOrderUnPayMsgJobDetail")
    public JobDetail regOrderUnPayMsgJobDetail() {
        return JobBuilder.newJob().ofType(RegOrderUnPayMsgJob.class)
                .storeDurably()
                .withIdentity("RegOrderUnPayMsgJob")
                .withDescription("未支付挂号订单通知")
                .build();
    }

    /**
     * 未支付挂号订单通知（1分钟一次）
     *
     * @param job
     * @return
     */
    @Bean
    public Trigger regOrderUnPayMsgJobTrigger(@Qualifier("regOrderUnPayMsgJobDetail") JobDetail job) {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("RegOrderUnPayMsgJob", QuartzGroupEnum.DEFAULT_GROUP.getVal())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 */1 * * * ?"))
                .build();
        return trigger;
    }

    /**
     * 挂号订单退费（3分钟一次）
     *
     * @return
     */
    @Bean("regOrderRefundPayFeeJobDetail")
    public JobDetail regOrderRefundPayFeeJobDetail() {
        return JobBuilder.newJob().ofType(RegOrderRefundPayFeeJob.class)
                .storeDurably()
                .withIdentity("RegOrderRefundPayFeeJob")
                .withDescription("挂号订单退费")
                .build();
    }

    /**
     * 挂号订单退费（3分钟一次）
     *
     * @param job
     * @return
     */
    @Bean
    public Trigger regOrderRefundPayFeeJobTrigger(@Qualifier("regOrderRefundPayFeeJobDetail") JobDetail job) {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("RegOrderRefundPayFeeJob", QuartzGroupEnum.DEFAULT_GROUP.getVal())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 */3 * * * ?"))
                .build();
        return trigger;
    }

    /**
     * 挂号订单退费查询（5分钟一次）
     *
     * @return
     */
    @Bean("regOrderRefundStatusQueryJobDetail")
    public JobDetail regOrderRefundStatusQueryJobDetail() {
        return JobBuilder.newJob().ofType(RegOrderRefundStatusQueryJob.class)
                .storeDurably()
                .withIdentity("RegOrderRefundStatusQueryJob")
                .withDescription("挂号订单退费查询")
                .build();
    }

    /**
     * 挂号订单退费查询（5分钟一次）
     *
     * @param job
     * @return
     */
    @Bean
    public Trigger regOrderRefundStatusQueryJobTrigger(@Qualifier("regOrderRefundStatusQueryJobDetail") JobDetail job) {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("RegOrderRefundStatusQueryJob", QuartzGroupEnum.DEFAULT_GROUP.getVal())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 */5 * * * ?"))
                .build();
        return trigger;
    }

    /**
     * 咨询单退费状态查询（5分钟一次）
     *
     * @return
     */
    @Bean("onlineOrderRefundStatusQueryJobDetail")
    public JobDetail onlineOrderRefundStatusQueryJobDetail() {
        return JobBuilder.newJob().ofType(OnlineOrderRefundStatusQueryJob.class)
                .storeDurably()
                .withIdentity("OnlineOrderRefundStatusQueryJob")
                .withDescription("咨询单退费状态查询")
                .build();
    }

    /**
     * 咨询单退费状态查询（5分钟一次）
     *
     * @param job
     * @return
     */
    @Bean
    public Trigger onlineOrderRefundStatusQueryJobTrigger(@Qualifier("onlineOrderRefundStatusQueryJobDetail") JobDetail job) {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("OnlineOrderRefundStatusQueryJob", QuartzGroupEnum.DEFAULT_GROUP.getVal())
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 */5 * * * ?"))
                .build();
        return trigger;
    }

}
